import java.io.IOException;
import java.net.ServerSocket;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
 

public class ServerMain
{
	private static Connection conn;
	private static Statement stat;
    public static void main(String[] args) throws Exception
    {
        ServerDB db = new ServerDB();
        db.createDatabase();
  
        //Setup connection
    	ServerSocket listener = null;
    	
    	try {
    		listener = new ServerSocket(9000);
    	} catch (IOException e) {
	        System.out.println("Could not listen on port 9000");
	        System.exit(-1);
    	}
    	
        //Connection loop
        while (true)
        {
        	ServerWorker w;
        	System.out.println("Waiting for connections...");
			try{
			    w = new ServerWorker(listener.accept());
			    Thread t = new Thread(w);
			    t.start();
			} catch (IOException e) {
			    System.out.println("Failed to accept connection");
			    System.exit(-1);
			}
        }
    }
    
    //This method accepts a username for the user who just logged in
    //and returns a 2xn array of that user's friend name-IP pairs
    //for friends with a non-null IP (meaning they are logged in currently)
    public static String[][] HandleLogin(String username, String userip) throws SQLException
    {
    	//set user's status to online
    	String myquery = "UPDATE `Users` SET IP='"+userip+"' WHERE Username='"+username;
    	stat.executeQuery(myquery);
    	System.out.println("User's IP set: "+userip);

    	//count the number of friends this user has
    	myquery = "SELECT COUNT FROM `Friends` WHERE User1 = '"+username+"'";
    	ResultSet rs = stat.executeQuery(myquery);
    	int x = rs.getInt(0);
    	System.out.println("Retrieved "+x+" friends for user "+username);

    	//create a 2xn array of this user's friends
    	String[][] onlinefriends = new String[x][2];

    	//acquire a ResultSet of the user's friends
    	myquery = "SELECT * FROM `Friends` WHERE User1 = '"+username+"'";
    	rs = stat.executeQuery(myquery);

    	//loop through ResultSet, storing friendname+IP pairs
    	for(int i = 0; i < x; i++)
    	{
    		onlinefriends[i][0] = rs.getString(0);
    		onlinefriends[i][1] = rs.getString(0);
    	}

    	return onlinefriends;
    }

    public static boolean HandleAddFriend(String username1, String username2) throws SQLException {
		return false;
    	//Add the relationship (username1 -> username2) to the database
    	//return true if successful, else false
    }
    
    public static boolean HandleRemFriend(String username1, String username2) throws SQLException {
		return false;
    	//remove the relationship (username1 -> username2) from database
    	//return true if successful, else false
    }
    
    public static boolean HandleCreateUser(String username) throws SQLException {
		return false;
    	//create a new database entry for username
    	//return true if successful, else false
    }
    
    public static boolean HandleExists(String username) throws SQLException {
		return false;
    	//check database to see if user exists
    	//return true if it does, else false
    }
} 
